<?php

namespace Biz\model\settlement;


class Settlement extends BaseAccountTransaction
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_settlement';

    const _DATABASE_ = 'shop_settlement';

    const _TABLE_NAME = 'settlement';

    public static function addData($data)
    {
        $ret = self::insert($data);
        if ($ret->errorCode() !== '00000') {
            throw new \Exception('数据库操作错误');
        }
        return true;
    }

    public static function getTransactionList($shopId, $startTime, $endTime, $orderNo, $limit = [], $order = [])
    {
        $where['AND']['status'] = self::STATUS_NORMAL;
        if ($shopId) {
            $where['AND']['shop_id'] = $shopId;
        }
        if ($startTime && $endTime) {
            $where['AND']['operate_time[>=]'] = $startTime;
            $where['AND']['operate_time[<=]'] = $endTime;
        }
        if (!is_null($orderNo)) {
            $where['AND']['order_no'] = $orderNo;
        }
        if ($limit) {
            $where['LIMIT'] = $limit;
        }
        $where['ORDER'] = ['operate_time' => 'DESC'];
        if ($order) {
            $where['ORDER'] = $order;
        }
        $list = self::select('*', $where);
        self::formatList($list);
        return $list;
    }

    public static function getTransactionCount($shopId, $startTime, $endTime, $orderNo)
    {
        $where = ['status' => self::STATUS_NORMAL];
        if ($shopId) {
            $where['shop_id'] = $shopId;
        }
        if ($startTime && $endTime) {
            $where['operate_time[>=]'] = $startTime;
            $where['operate_time[<=]'] = $endTime;
        }
        if (!is_null($orderNo)) {
            $where['order_no'] = $orderNo;
        }
        return self::count('*', $where);
    }

    public static function getConfirmOrderList($orderNos, $startTime, $endTime)
    {
        $where['AND']['order_no'] = $orderNos;
        $where['AND']['status'] = self::STATUS_NORMAL;
        if ($startTime && $endTime) {
            $where['operate_time[>=]'] = $startTime;
            $where['operate_time[<=]'] = $endTime;
        }
        $where['ORDER'] = ['operate_time' => 'DESC'];
        $list = self::select('*', $where);
        self::formatList($list);
        return $list;
    }

    public static function getConfirmOrderNos($shopId, $startTime, $endTime, $limit = [], $order = [])
    {
        if ($shopId) {
            $where['AND'] = ['shop_id' => $shopId];
        }
        $where['AND']['status'] = self::STATUS_NORMAL;
        $where['AND']['operate_time[>=]'] = $startTime;
        $where['AND']['operate_time[<=]'] = $endTime;
        $where['AND']['transaction_type'] = self::TRANSACTION_TYPE_ORDER_SETTLEMENT_RMB;
        if ($limit) {
            $where['LIMIT'] = $limit;
        }
        $where['ORDER'] = ['operate_time' => 'DESC'];
        if ($order) {
            $where['ORDER'] = $order;
        }
        $list = self::select('order_no', $where);
        return $list;
    }

    public static function getConfirmOrderCount($shopId, $startTime, $endTime)
    {
        $where = [
            'status' => self::STATUS_NORMAL,
            'operate_time[>=]' => $startTime,
            'operate_time[<=]' => $endTime,
            'transaction_type' => self::TRANSACTION_TYPE_ORDER_SETTLEMENT_RMB,
        ];
        if ($shopId) {
            $where['shop_id'] = $shopId;
        }
        $count = self::count('*', $where);
        return $count;
    }

    public static function getTransList($select = '*', $params, $limit = [], $order = [])
    {
        $where['AND'] = $params;
        $where['AND']['status'] = isset($params['status']) ? $params['status'] : self::STATUS_NORMAL;
        if ($limit) {
            $where['LIMIT'] = $limit;
        }
        $where['ORDER'] = ['operate_time' => 'DESC'];
        if ($order) {
            $where['ORDER'] = $order;
        }
        $list = self::select($select, $where);
        if ($select == '*') {
            self::formatList($list);
        }
        return $list;
    }

    public static function getTransListCount($where)
    {
        $where['status'] = isset($where['status']) ? $where['status'] : self::STATUS_NORMAL;
        return self::count('*', $where);
    }

    public static function getOtherTransList($shopId, $startTime, $endTime, $limit = [], $order = [])
    {
        $where['AND'] = ['shop_id' => $shopId];
        $where['AND']['operate_time[>=]'] = $startTime;
        $where['AND']['operate_time[<=]'] = $endTime;
        $where['AND']['order_no'] = '';
        $where['AND']['transaction_type'] = [
            self::TRANSACTION_TYPE_COMMISSION_ADJUSTMENT,
            self::TRANSACTION_TYPE_HHZ_ALLOWANCE_ADJUSTMENT,
            self::TRANSACTION_TYPE_DEPOSIT_ADD_GOODS_AMOUNT,
        ];//运营调整，保证金补货款
        $where['AND']['status'] = self::STATUS_NORMAL;
        if ($limit) {
            $where['LIMIT'] = $limit;
        }
        $where['ORDER'] = ['operate_time' => 'DESC'];
        if ($order) {
            $where['ORDER'] = $order;
        }
        $list = self::select('*', $where);
        self::formatList($list);
        $count = self::count('*', $where['AND']);
        return ['list' => $list, 'count' => $count];
    }

    public static function formatList(&$list)
    {
        foreach ($list as &$row) {
            self::formatInfo($row);
        }
    }

    public static function formatInfo(&$info)
    {
        if (empty($info)) {
            return;
        }
        if ($info['operate_type'] == self::OPERATE_TYPE_SYS) {
            $info['operator'] = self::$operateTypeDesc[self::OPERATE_TYPE_SYS];
        } else if ($info['operate_type'] == self::OPERATE_TYPE_ADMIN) {
            $info['operator'] = self::$operateTypeDesc[self::OPERATE_TYPE_ADMIN];
        } else if ($info['operate_type'] == self::OPERATE_TYPE_SHOP) {
            $info['operator'] = self::$operateTypeDesc[self::OPERATE_TYPE_SHOP];
        } else if ($info['operate_type'] == self::OPERATE_TYPE_USER) {
            $info['operator'] = self::$operateTypeDesc[self::OPERATE_TYPE_USER];
        }
        foreach ($info as $key => &$value) {
            switch ($key) {
                case 'transaction_type':
                    $info['transaction_type_desc'] = self::$transactionTypeDesc[$value];
                    break;
            }
        }

    }
}
